package com.baosight.ice;


import com.baosight.ice.observer.*;
import com.zeroc.Ice.*;
import com.zeroc.IceGrid.*;
import com.zeroc.IceGrid.LocatorPrx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;

public class IceGUI {
    public static Logger logger= LoggerFactory.getLogger(IceGUI.class);
    public static void main (String args[]) throws PermissionDeniedException, ObserverAlreadyRegisteredException {
        System.out.println(Runtime.getRuntime().availableProcessors());
        Communicator communicator = Util.initialize(new String[]{
                });
        String ip="10.25.25.129";
        String port="12000";
        String userName="1";
        String password="";
//        final String finderStr = "Ice/" + (info.getDirect() ? "LocatorFinder" : "RouterFinder") + ":" +
//                (info.getDefaultEndpoint() ?
//                        ((info.getSSL() ? "ssl" : "tcp") + " -h " + info.getHost() + " -p " + info.getPort()) :
//                        info.getEndpoint());
        //发现主节点名字
        System.out.println("发现主节点名字");
        String finderStr= MessageFormat.format(
                "Ice/LocatorFinder:tcp -h {0} -p {1} -t 6000",ip,port
        ) ;
        LocatorFinderPrx locatorFinderPrx = LocatorFinderPrx.uncheckedCast(
                communicator.stringToProxy(finderStr));
        String instanceName = locatorFinderPrx.getLocator().ice_getIdentity().category;
        //Locator And Registry
        System.out.println("Locator And Registry");
        LocatorPrx locatorPrx= LocatorPrx.checkedCast(
                locatorFinderPrx.ice_identity(new Identity("Locator",instanceName)));
        RegistryPrx localRegistry = locatorPrx.getLocalRegistry();
        //DefaultRouter and routerRegistry
        System.out.println("DefaultRouter and routerRegistry");
        communicator.setDefaultLocator(locatorPrx);
        com.zeroc.Ice.ObjectAdapter colloc = communicator.createObjectAdapter("");
        ObjectPrx router = colloc.addWithUUID(new ReuseConnectionRouter(locatorPrx));
        communicator.setDefaultRouter(RouterPrx.uncheckedCast(router));
        RegistryPrx registryPrx = localRegistry.ice_router(communicator.getDefaultRouter());
        //AdminSession
        System.out.println("AdminSession");
        registryPrx.ice_preferSecure(true);
        AdminSessionPrx adminSession = registryPrx.createAdminSession(userName, password);
        String replicaName = adminSession.getReplicaName();//Mater ==>>Logged into Master Registry else Logged into Slave Registry
        //admin And adminCallBackTemplate
        System.out.println("admin And adminCallBackTemplate");
        AdminPrx admin = adminSession.getAdmin();
        ObjectPrx adminCallbackTemplate = adminSession.getAdminCallbackTemplate();
        String publisherEndpoints=null;
        for(com.zeroc.Ice.Endpoint endpoint : adminCallbackTemplate.ice_getEndpoints())
        {
            String endpointString = endpoint.toString();
            if(publisherEndpoints == null)
            {
                publisherEndpoints = endpointString;
            }
            else
            {
                publisherEndpoints += ":" + endpointString;
            }
        }
        communicator.getProperties().setProperty("CallbackAdapter.PublishedEndpoints",publisherEndpoints);
        String serverAdminCategory = admin.getServerAdminCategory();
        //registerObservers adapter
        System.out.println("registerObservers adapter");
        String category ="observer";
        ObjectAdapter adapter;
        String adapterName=adminCallbackTemplate.ice_getIdentity().category==null?"":"CallbackAdapter";
        adapter=communicator.createObjectAdapter(adapterName);
        adapter.activate();
        adminSession.ice_getConnection().setAdapter(adapter);
        //create servants and proxies
        Identity applicationObserverIdentity=new Identity(
                "application-" + java.util.UUID.randomUUID().toString(),
                category
        );
        Identity adapterObserverIdentity=new Identity(
                "adapter-" + java.util.UUID.randomUUID().toString(),
                category
        );
        Identity objectObserverIdentity=new Identity(
                "object-" + java.util.UUID.randomUUID().toString(),
                category
        );
        Identity registryObserverIdentity=new Identity(
                "registry-" + java.util.UUID.randomUUID().toString(),
                category
        );
        Identity nodeObserverIdentity=new Identity(
                "node-" + java.util.UUID.randomUUID().toString(),
                category
        );
        ApplicationObserverI applicationObserverServant=new ApplicationObserverI();
        ApplicationObserverPrx applicationObserver=ApplicationObserverPrx.checkedCast(adapter.add(
                applicationObserverServant,applicationObserverIdentity
        ));
        AdapterObserverPrx adapterObserver=AdapterObserverPrx.checkedCast(adapter.add(
                new AdapterObserverI(),adapterObserverIdentity
        ));
        ObjectObserverPrx objectObserver =ObjectObserverPrx.checkedCast(adapter.add(
                new ObjectObserverI(),objectObserverIdentity
        ));
        RegistryObserverPrx registryObserver=RegistryObserverPrx.checkedCast(adapter.add(
                new RegistryObserverI(),registryObserverIdentity
        ));
        NodeObserverPrx nodeObserver=NodeObserverPrx.checkedCast(adapter.add(
                new NodeObserverI(),nodeObserverIdentity
        ));
        adminSession.setObserversByIdentity(registryObserverIdentity,
                nodeObserverIdentity,
                applicationObserverIdentity,
                adapterObserverIdentity,
                objectObserverIdentity);
        System.out.println("end");
    }

    static private class ReuseConnectionRouter implements com.zeroc.Ice.Router
    {
        public
        ReuseConnectionRouter(com.zeroc.Ice.ObjectPrx proxy)
        {
            _clientProxy = proxy;
        }

        @Override
        public com.zeroc.Ice.Router.GetClientProxyResult
        getClientProxy(com.zeroc.Ice.Current current)
        {
            return new com.zeroc.Ice.Router.GetClientProxyResult(_clientProxy, false);
        }

        @Override
        public com.zeroc.Ice.ObjectPrx
        getServerProxy(com.zeroc.Ice.Current current)
        {
            return null;
        }

        @Override
        public com.zeroc.Ice.ObjectPrx[]
        addProxies(com.zeroc.Ice.ObjectPrx[] proxies, com.zeroc.Ice.Current current)
        {
            return new com.zeroc.Ice.ObjectPrx[0];
        }

        private final com.zeroc.Ice.ObjectPrx _clientProxy;
    }
}
